package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"yzy6.26exam/api/request"
	"yzy6.26exam/golbal"
	"yzy6.26exam/internal/moble"
)

func VideoList(context *gin.Context) {
	var video_list request.VideoList
	if err := context.ShouldBind(&video_list); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{
			"code":    200,
			"message": "解析失败",
			"data":    err.Error(),
		})
		return
	}
	var Videos []moble.Video

	if err := golbal.DB.Offset((video_list.Page - 1) * 10).Order("created_at desc").Limit(10).Find(&Videos).Error; err != nil {
		context.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "查询失败",
			"data":    err.Error(),
		})
		return
	}
	var total int64
	if err := golbal.DB.Model(&moble.Video{}).Count(&total).Error; err != nil {
		context.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "查询失败",
			"data":    err.Error(),
		})
		return
	}
	context.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "查询成功",
		"data": struct {
			Videos []moble.Video
			Total  int64
		}{
			Videos: Videos,
			Total:  total,
		},
	})
	return
}
